package cn.iocoder.yudao.core.sensitive.stragegy;


import cn.iocoder.yudao.core.sensitive.SensitiveInfoUtils;
import cn.iocoder.yudao.core.sensitive.SensitiveType;

import java.util.HashMap;
import java.util.Map;
import java.util.function.Function;

import static cn.iocoder.yudao.core.sensitive.SensitiveDefaultLengthEnum.FIXED_PHONE;
import static cn.iocoder.yudao.core.sensitive.SensitiveDefaultLengthEnum.MOBILE;

public class SensitiveStrategy implements ISensitiveStrategy {
    private static final Map<String, Function<String, String>> STRATEGY_FUNCTION_MAP;

    static {
        STRATEGY_FUNCTION_MAP = new HashMap<>();
        STRATEGY_FUNCTION_MAP.put(SensitiveType.chineseName, (source -> SensitiveInfoUtils.patternReplace(source, "(?<=.{1}).", "*")));
        STRATEGY_FUNCTION_MAP.put(SensitiveType.address, (source -> SensitiveInfoUtils.patternReplace(source, "(.{5}).+(.{4})", "$1*****$2")));
        STRATEGY_FUNCTION_MAP.put(SensitiveType.bankCard, (source -> SensitiveInfoUtils.patternReplace(source, "(?<=\\w{4})\\w(?=\\w{4})", "*")));
        STRATEGY_FUNCTION_MAP.put(SensitiveType.email, (source -> SensitiveInfoUtils.email(source, 1)));
        STRATEGY_FUNCTION_MAP.put(SensitiveType.idCard, (source -> SensitiveInfoUtils.patternReplace(source, "(?<=\\w{4})\\w(?=\\w{4})", "*")));
        STRATEGY_FUNCTION_MAP.put(SensitiveType.mobile, (source -> SensitiveInfoUtils.mobilePhone(source, MOBILE.getBegin(), MOBILE.getEnd())));
        STRATEGY_FUNCTION_MAP.put(SensitiveType.fixed_phone, (source -> SensitiveInfoUtils.fixedPhone(source, FIXED_PHONE.getEnd())));
        STRATEGY_FUNCTION_MAP.put(SensitiveType.password, (source -> SensitiveInfoUtils.patternReplace(source, "(?<=).", "*")));
    }
    @Override
    public Map<String, Function<String, String>> getStrategyFunctionMap() {
        return STRATEGY_FUNCTION_MAP;
    }


}